//
//  SendMessageViewController.m
//  SinaWeiBo
//
//  Created by Hunk on 11-3-24.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "SendMessageViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "WeiboClient.h"
#import "OAuthEngine.h"
#import "MyLoadingView.h"

@implementation SendMessageViewController
@synthesize journeyName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    SAFE_RELEASE(m_pWordCountLabel)
    SAFE_RELEASE(m_pMessageTextView)
    SAFE_RELEASE(sendBarButtonItem)
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"分享到新浪微博";
    
    // Background view
    UIImageView* bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    [bgImageView setImage:[[UIImage imageNamed:@"CellBackground.png"]
                           stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
    [self.view addSubview:bgImageView];
    [bgImageView release];
    
    // Send button
    sendBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送"
                                                         style:UIBarButtonItemStyleDone
                                                        target:self 
                                                        action:@selector(sendButtonHandle:)];
    sendBarButtonItem.enabled = NO;
    self.navigationItem.rightBarButtonItem = sendBarButtonItem;
    
    
    // UITextView
    m_pMessageTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 300, 160)];
	m_pMessageTextView.backgroundColor = [UIColor whiteColor];
	m_pMessageTextView.textColor = [UIColor blackColor];
	m_pMessageTextView.font = [UIFont fontWithName:@"Arial" size:18];
	m_pMessageTextView.delegate = self;
	m_pMessageTextView.keyboardType = UIKeyboardTypeDefault;
    m_pMessageTextView.keyboardAppearance = UIKeyboardAppearanceAlert;
	m_pMessageTextView.scrollEnabled = YES;
	m_pMessageTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    NSString* defaultString = [NSString stringWithFormat:@"#团游世界# \"%@\"", journeyName];
    m_pMessageTextView.text = defaultString;
	m_pMessageTextView.layer.cornerRadius = 6;
	m_pMessageTextView.layer.borderWidth = 1;
	m_pMessageTextView.layer.borderColor = [[UIColor grayColor] CGColor];
	m_pMessageTextView.layer.masksToBounds = YES;
    [m_pMessageTextView becomeFirstResponder];
	[self.view addSubview:m_pMessageTextView];
    
    // Word count label
    m_pWordCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,125,100,22)];
    NSString* stringLabel = [NSString stringWithFormat:@"%d/140个字", [defaultString length]];
	m_pWordCountLabel.text = stringLabel;
	m_pWordCountLabel.textColor = [UIColor blackColor];
	m_pWordCountLabel.backgroundColor = [UIColor clearColor];
	m_pWordCountLabel.textAlignment = UITextAlignmentRight;
	[self.view addSubview:m_pWordCountLabel];
}

#pragma mark -
#pragma mark UITextViewDelegate Methods
- (void)textViewDidChange:(UITextView *)textView
{
    if(textView.text.length > 0)
    {
        sendBarButtonItem.enabled = YES;
    }
    else
    {
        sendBarButtonItem.enabled = NO;
    }
    m_pWordCountLabel.text = [NSString stringWithFormat:@"%d/140个字",textView.text.length];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if(text.length == 0) 
        return YES;
	
	if(textView.text.length < 140)
		return YES;

	return NO;
}

#pragma mark -
#pragma mark Cancel button handle
- (void)leftBarButtonItemHandle:(id)sender
{    
    [self dismissModalViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark Send message button handle
- (void)sendButtonHandle:(id)sender
{
    [[MyLoadingView sharedMyLoadingView] startLoadingAnimationWithName:@"正在发送"];
    
    [m_pMessageTextView resignFirstResponder];
    
    WeiboClient* weiboClient = [[WeiboClient alloc] initWithTarget:self 
                                                            engine:[OAuthEngine currentOAuthEngine] 
                                                            action:@selector(postStatusDidSucceed:object:)];
    [weiboClient post:m_pMessageTextView.text];
}

- (void)postStatusDidSucceed:(WeiboClient*)sender object:(NSObject*)object
{   
    [[MyLoadingView sharedMyLoadingView] stopLoadingAnimation];
    
    if (sender.hasError) 
    {
        [sender alert];	
    }
    else
    {
        ALERT_GENERAL(@"微博发送成功")
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
